﻿using JZ.JiangSu.Common.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Threading.Tasks;
using Unity;

namespace JZ.JiangSu.WcfService.WcfIocInit
{
    public class IocInstanceProvider: IInstanceProvider
    {
        Type _serviceType;
        IUnityContainer _container;

        public IocInstanceProvider(Type serviceType)
        {
            _serviceType = serviceType;
            _container = IocContainerProvider.IocContainer;
        }


        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            return _container.Resolve(_serviceType);
        }

        public object GetInstance(InstanceContext instanceContext)
        {
            return GetInstance(instanceContext, null);
        }

        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
            if (instance is IDisposable)
                ((IDisposable)instance).Dispose();
        }
    }
}
